package models

import (

	"gopkg.in/mgo.v2/bson"
	"kukukaki/db"
	"log"
	"time"
)


type SystemCache struct {
	UsersNum int       `json:"usersNum"  bson:"usersNum"`
	AllUseNum   int    `json:"allUseNum"  bson:"allUseNum"`
	AllWarningNum int   `json:"allWarningNum"  bson:"allWarningNum"`
	UpdateTime string   ` json:"updateTime" bson:"updateTime"`
}

var SysCache  SystemCache


func SysCacheInit() (err error) {
	log.Println("get SysCache")
	con := db.GetDataBase().C("SysInfo")
	err = con.Find(bson.M{"_id": "sys"}).One(&SysCache)

	if  SysCache.UpdateTime == "" {
		log.Println("Insert sysinfo")
		SysCache.UpdateTime = time.Now().Format("2006-1-2 15:04:05")
		SysCache.AllUseNum = 0
		SysCache.AllWarningNum = 0
		SysCache.UsersNum = 0
		err = con.Insert(&SysCache)
	}
	return err 
}



func UpdateSysInfo() (err error) {
        con := db.GetDataBase().C("SysInfo")
         //更新
        err = con.Update(bson.M{"_id": "sys"},bson.M{"$set": SysCache})
        
        return err
}